# 基于官方 Ubuntu 镜像
FROM ubuntu:22.04

# 安装必要的依赖
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release \
    git wget telnet make

RUN wget https://go.dev/dl/go1.23.4.linux-amd64.tar.gz
RUN rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz && rm go1.23.4.linux-amd64.tar.gz
ENV PATH=$PATH:/usr/local/go/bin
RUN go version

# 添加 Docker 官方 GPG 密钥
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg  | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# 设置 Docker APT 源
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list

# 安装 Docker CE 和 CLI
RUN apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io \
    && rm -rf /var/lib/apt/lists/*

# 安装 Docker Compose
RUN curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname  -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
    chmod +x /usr/local/bin/docker-compose

# 验证 Docker Compose 是否安装成功
RUN docker-compose --version


COPY ./scripts-in-dind/install-fabric.sh /install-fabric.sh
RUN chmod +x ./install-fabric.sh
RUN ./install-fabric.sh -f 2.2.7 s b

# replace the script to disable govendoring in default fabric-samples
RUN rm -f /fabric-samples/test-network/scripts/deployCC.sh
COPY fabric-samples/test-network/scripts/deployCC-offnet.sh /fabric-samples/test-network/scripts/deployCC.sh
RUN chmod +x /fabric-samples/test-network/scripts/deployCC.sh

COPY ./chaincode-go-token /fabric-samples/asset-transfer/chaincode-go
COPY ./chaincode-go-evidence /fabric-samples/evidence-persistance/chaincode-go-evidence

COPY ./scripts-in-dind/start-network.sh /fabric-samples/start-network.sh
RUN chmod +x /fabric-samples/start-network.sh

COPY ./scripts-in-dind/prepare-all.sh prepare-all.sh
RUN chmod +x prepare-all.sh

COPY ./backend /backend
RUN cd /backend/cmd/ && go build 

# COPY ./images /images
COPY ./scripts-in-dind/prepare-all-offnet.sh /prepare-all-offnet.sh
RUN chmod +x /prepare-all-offnet.sh

COPY ./fabric-zkrollup /fabric-zkrollup
RUN cd /fabric-zkrollup && make build TARGET=storage && make build TARGET=transfer

CMD ["dockerd"]
